﻿using MESMian.Common;
using MESMian.Context;
using MESMian.Test;
using Microsoft.EntityFrameworkCore;
using Model;
using NPOI.SS.Formula.Functions;

namespace MESMian.Forms.DeviceSpec
{
    public partial class DeviceSpecAddEdit : AddEditBase
    {
        Model.DeviceSpecs? editModel;
        public DeviceSpecAddEdit(int primaryId)
        {
            InitializeComponent();
            this.editId = primaryId;
            Load += DeviceSpecAddEdit_Load;
            btnSave.Click += BtnSave_Click;
        }

        private void DeviceSpecAddEdit_Load(object? sender, EventArgs e)
        {
            if (editId > 0)
            {
                Text = "设备规格-编辑";
                using (var mtx = new MyContext())
                {
                    editModel = mtx.DeviceSpecs.Find(editId);
                    if (editModel != null)
                    {
                        txtDeviceSpecName.Text = editModel.DeviceSpecName;
                    }
                }
            }
            else
            {
                Text = "设备规格-添加";
            }
        }


        private void BtnSave_Click(object? sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtDeviceSpecName.Text))
            {
                MessageBox.Show("设备规格名称不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (var mtx = new MyContext())
            {

                if (editId > 0)
                {
                    if (editModel != null)
                    {
                        DeviceSpecs? data = mtx.DeviceSpecs.SingleOrDefault(item =>
                             item.DeviceSpecName == txtDeviceSpecName.Text &&
                             item.DeviceSpecName != editModel!.DeviceSpecName);

                        if (data != null)
                        {
                            MessageBox.Show("设备规格名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        editModel.DeviceSpecName = txtDeviceSpecName.Text;
                        editModel.LastUpdateUserId = LoginInfo.Id;
                        editModel.LastUpdateTime = DateTime.Now;
                        mtx.Update(editModel);

                    }
                }
                else
                {
                    var query = mtx.DeviceSpecs.Where(item =>
                              item.DeviceSpecName == txtDeviceSpecName.Text);
                    if (query.Count() >= 1)
                    {
                        MessageBox.Show("设备规格名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    var model = new DeviceSpecs()
                    {
                        DeviceSpecName = txtDeviceSpecName.Text,
                        CreateUserId = LoginInfo.Id,
                    };
                    mtx.Add(model);
                }


                mtx.SaveChanges();
                DialogResult = DialogResult.OK;
            }
        }
    }
}
